package com.jiepos.mpos.common.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.entity.MemReviewEntity;
import com.jiepos.mpos.core.dao.BaseDao;
import com.jiepos.mpos.core.util.IDFactoryUtil;


@Repository
public class MemReviewDao extends BaseDao<MemReviewEntity> {

	public Integer getMemberReviewNum(String sql){
		return jdbcTemplate.queryForInt(sql);
	}
	/**
	 * 会员实名认证审核通过
	 * @param audit_comments 审核意见
	 * @param custom_uuid 会员标识
	 * @param bankcard_uuid 结算卡标识
	 * @param map
	 * @return
	 */
	@SuppressWarnings("resource")
	public boolean reviewCustomer(String audit_comments, String custom_uuid, String bankcard_uuid, Map<String, Object> map, boolean isPass){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		/*操作标识位*/
		boolean isSuccess = false;
		try {
			conn = jdbcTemplate.getDataSource().getConnection();
			/*改为手动提交事务*/
			conn.setAutoCommit(false);
			/*查询会员信息*/
			StringBuffer sql = new StringBuffer("SELECT * FROM T_APP_CUSTOM_INF WHERE REALNAME_AUDIT_ST = ? AND CUSTOM_UUID = ? FOR UPDATE");
			ps = conn.prepareStatement(sql.toString());
			ps.setString(1, SystemConstant.REVIEW_ST_0);
			ps.setString(2, custom_uuid);
			rs = ps.executeQuery();
			if(rs.next()){
				/*更新会员数据*/
				int i = 1;
				sql = new StringBuffer("UPDATE T_APP_CUSTOM_INF SET");
				/*审核通过，更新实名认证状态和通过时间*/
				if(isPass){
					sql.append(" REALNAME_AUTH_ST = ?, REALNAME_AUTH_TS = ?,");
				}
				sql.append(" REALNAME_AUDIT_ST = ?, LAST_OPER_IN = ?, REC_UPD_TS= ?, REC_UPD_USR_ID = ? WHERE CUSTOM_UUID = ?");
				ps = conn.prepareStatement(sql.toString());
				/*审核通过参数*/
				if(isPass){
					ps.setString(i++, SystemConstant.AUTH_ST_1);
					ps.setTimestamp(i++, new Timestamp(System.currentTimeMillis()));
					ps.setString(i++, SystemConstant.REVIEW_ST_1);
				}else{
					ps.setString(i++, SystemConstant.REVIEW_ST_2);
				}
				ps.setString(i++, SystemConstant.OPER_UPDATE);
				ps.setTimestamp(i++, new Timestamp(System.currentTimeMillis()));
				ps.setString(i++, String.valueOf(map.get(SystemConstant.SESSION_LOGIN_USER)));
				ps.setString(i++, custom_uuid);
				ps.executeUpdate();
				/*变更结算卡*/
				i = 1;
				sql = new StringBuffer("UPDATE T_APP_CUSTOM_BANKCARD SET LAST_OPER_IN = ?, REC_UPD_TS= ?, REC_UPD_USR_ID = ?, AUDIT_ST = ? ");
				if(isPass){
					sql.append("WHERE BANKCARD_UUID = ?");
				}else{
					sql.append("WHERE REC_ST = ? AND ACCT_ATTR = ? AND CUSTOM_UUID = ?");
				}
				ps = conn.prepareStatement(sql.toString());
				ps.setString(i++, SystemConstant.OPER_UPDATE);
				ps.setTimestamp(i++, new Timestamp(System.currentTimeMillis()));
				ps.setString(i++, String.valueOf(map.get(SystemConstant.SESSION_LOGIN_USER)));
				if(isPass){
					ps.setString(i++, SystemConstant.REVIEW_ST_1);
					ps.setString(i++, bankcard_uuid);
				}else{
					ps.setString(i++, SystemConstant.REVIEW_ST_2);
					ps.setString(i++, SystemConstant.REC_ST_1);
					ps.setString(i++, SystemConstant.ACCT_ATTR_0);
					ps.setString(i++, custom_uuid);
				}
				ps.executeUpdate();
				/*变更信用卡*/
				i = 1;
				sql= new StringBuffer("UPDATE T_APP_CUSTOM_BANKCARD SET AUDIT_ST = ?, LAST_OPER_IN = ?, REC_UPD_TS= ?, REC_UPD_USR_ID = ? WHERE REC_ST = ? AND ACCT_ATTR = ? AND CUSTOM_UUID = ?");
				ps = conn.prepareStatement(sql.toString());
				if(isPass){
					ps.setString(i++, SystemConstant.REVIEW_ST_1);
				}else{
					ps.setString(i++, SystemConstant.REVIEW_ST_2);
				}
				ps.setString(i++, SystemConstant.OPER_UPDATE);
				ps.setTimestamp(i++, new Timestamp(System.currentTimeMillis()));
				ps.setString(i++, String.valueOf(map.get(SystemConstant.SESSION_LOGIN_USER)));
				ps.setString(i++, SystemConstant.REC_ST_1);
				ps.setString(i++, SystemConstant.ACCT_ATTR_1);
				ps.setString(i++, custom_uuid);
				ps.executeUpdate();
				/*增加工作流记录*/
				i = 1;
				sql = new StringBuffer("INSERT INTO T_APP_WORKFLOW_TRACE("
				+ "WORKFLOW_UUID, "
				+ "LOGIN_NAME, "
				+ "SYSTEM_TYPE, "
				+ "SYSTEM_KEY, "
				+ "OLD_STATE, "
				+ "NEW_STATE, "
				+ "STATE_OPERATE, "
				+ "AUDIT_COMMENTS, "
				+ "REC_ST, "
				+ "LAST_OPER_IN, "
				+ "REC_UPD_USR_ID, "
				+ "REC_UPD_TS, "
				+ "REC_CRT_TS) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)");
				ps = conn.prepareStatement(sql.toString());
				ps.setString(i++, IDFactoryUtil.getUUID());
				ps.setString(i++, String.valueOf(map.get(SystemConstant.SESSION_LOGIN_USER)));
				ps.setString(i++, "T_APP_CUSTOM_INF");
				ps.setString(i++, custom_uuid);
				ps.setString(i++, SystemConstant.WORKFLOW_NODE_0);
				if(isPass){
					ps.setString(i++, SystemConstant.WORKFLOW_NODE_1);
					ps.setString(i++, SystemConstant.STATE_CHANGE_1);
				}else{
					ps.setString(i++, SystemConstant.WORKFLOW_NODE_2);
					ps.setString(i++, SystemConstant.STATE_CHANGE_2);
				}
				ps.setString(i++, audit_comments);
				ps.setString(i++, SystemConstant.REC_ST_1);
				ps.setString(i++, SystemConstant.OPER_ADD);
				ps.setString(i++, String.valueOf(map.get(SystemConstant.SESSION_LOGIN_USER)));
				ps.setTimestamp(i++, new Timestamp(System.currentTimeMillis()));
				ps.setTimestamp(i++, new Timestamp(System.currentTimeMillis()));
				ps.executeUpdate();
				/*操作标识位置为成功*/
				isSuccess = true;
			}
			/*提交事务*/
			conn.commit();
		} catch (Exception e) {
			e.printStackTrace();
			if(conn != null){
				try {
					conn.rollback();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		} finally{
			if(rs != null){
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(ps != null){
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(conn != null){
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return isSuccess;
	}
}
